﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace HospitalManagement.DAL
{
	/// <summary>
	/// 数据访问类:PrimaryTumor
	/// </summary>
	public partial class PrimaryTumor
	{
		public PrimaryTumor()
		{}
		#region  Method

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("SysNo", "PrimaryTumor"); 
		}


		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int SysNo)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from PrimaryTumor");
			strSql.Append(" where SysNo="+SysNo+" ");
			return DbHelperSQL.Exists(strSql.ToString());
		}

		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(HospitalManagement.Model.PrimaryTumor model)
		{
			StringBuilder strSql=new StringBuilder();
			StringBuilder strSql1=new StringBuilder();
			StringBuilder strSql2=new StringBuilder();
			if (model.PatientSysNo != null)
			{
				strSql1.Append("PatientSysNo,");
				strSql2.Append(""+model.PatientSysNo+",");
			}
			if (model.HospitalName != null)
			{
				strSql1.Append("HospitalName,");
				strSql2.Append("'"+model.HospitalName+"',");
			}
			if (model.SurgicalApproach != null)
			{
				strSql1.Append("SurgicalApproach,");
				strSql2.Append("'"+model.SurgicalApproach+"',");
			}
			if (model.ResectionSurgery != null)
			{
				strSql1.Append("ResectionSurgery,");
				strSql2.Append("'"+model.ResectionSurgery+"',");
			}
			if (model.Tumor != null)
			{
				strSql1.Append("Tumor,");
				strSql2.Append("'"+model.Tumor+"',");
			}
			if (model.TumorTypes != null)
			{
				strSql1.Append("TumorTypes,");
				strSql2.Append("'"+model.TumorTypes+"',");
			}
			if (model.SickNumber != null)
			{
				strSql1.Append("SickNumber,");
				strSql2.Append("'"+model.SickNumber+"',");
			}
			if (model.SickText != null)
			{
				strSql1.Append("SickText,");
				strSql2.Append("'"+model.SickText+"',");
			}
			if (model.NecrosisRate != null)
			{
				strSql1.Append("NecrosisRate,");
				strSql2.Append("'"+model.NecrosisRate+"',");
			}
			if (model.NecrosisRateVal != null)
			{
				strSql1.Append("NecrosisRateVal,");
				strSql2.Append("'"+model.NecrosisRateVal+"',");
			}
			if (model.Surgeon != null)
			{
				strSql1.Append("Surgeon,");
				strSql2.Append("'"+model.Surgeon+"',");
			}
			if (model.Disabled != null)
			{
				strSql1.Append("Disabled,");
				strSql2.Append(""+model.Disabled+",");
			}
			if (model.Indate != null)
			{
				strSql1.Append("Indate,");
				strSql2.Append("'"+model.Indate+"',");
			}
			if (model.InUser != null)
			{
				strSql1.Append("InUser,");
				strSql2.Append("'"+model.InUser+"',");
			}
			if (model.EditDate != null)
			{
				strSql1.Append("EditDate,");
				strSql2.Append("'"+model.EditDate+"',");
			}
			if (model.EditUser != null)
			{
				strSql1.Append("EditUser,");
				strSql2.Append("'"+model.EditUser+"',");
			}
			strSql.Append("insert into PrimaryTumor(");
			strSql.Append(strSql1.ToString().Remove(strSql1.Length - 1));
			strSql.Append(")");
			strSql.Append(" values (");
			strSql.Append(strSql2.ToString().Remove(strSql2.Length - 1));
			strSql.Append(")");
			strSql.Append(";select @@IDENTITY");
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}

		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(HospitalManagement.Model.PrimaryTumor model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update PrimaryTumor set ");
			if (model.PatientSysNo != null)
			{
				strSql.Append("PatientSysNo="+model.PatientSysNo+",");
			}
			else
			{
				strSql.Append("PatientSysNo= null ,");
			}
			if (model.HospitalName != null)
			{
				strSql.Append("HospitalName='"+model.HospitalName+"',");
			}
			else
			{
				strSql.Append("HospitalName= null ,");
			}
			if (model.SurgicalApproach != null)
			{
				strSql.Append("SurgicalApproach='"+model.SurgicalApproach+"',");
			}
			else
			{
				strSql.Append("SurgicalApproach= null ,");
			}
			if (model.ResectionSurgery != null)
			{
				strSql.Append("ResectionSurgery='"+model.ResectionSurgery+"',");
			}
			else
			{
				strSql.Append("ResectionSurgery= null ,");
			}
			if (model.Tumor != null)
			{
				strSql.Append("Tumor='"+model.Tumor+"',");
			}
			else
			{
				strSql.Append("Tumor= null ,");
			}
			if (model.TumorTypes != null)
			{
				strSql.Append("TumorTypes='"+model.TumorTypes+"',");
			}
			else
			{
				strSql.Append("TumorTypes= null ,");
			}
			if (model.SickNumber != null)
			{
				strSql.Append("SickNumber='"+model.SickNumber+"',");
			}
			else
			{
				strSql.Append("SickNumber= null ,");
			}
			if (model.SickText != null)
			{
				strSql.Append("SickText='"+model.SickText+"',");
			}
			else
			{
				strSql.Append("SickText= null ,");
			}
			if (model.NecrosisRate != null)
			{
				strSql.Append("NecrosisRate='"+model.NecrosisRate+"',");
			}
			else
			{
				strSql.Append("NecrosisRate= null ,");
			}
			if (model.NecrosisRateVal != null)
			{
				strSql.Append("NecrosisRateVal='"+model.NecrosisRateVal+"',");
			}
			else
			{
				strSql.Append("NecrosisRateVal= null ,");
			}
			if (model.Surgeon != null)
			{
				strSql.Append("Surgeon='"+model.Surgeon+"',");
			}
			else
			{
				strSql.Append("Surgeon= null ,");
			}
			if (model.Disabled != null)
			{
				strSql.Append("Disabled="+model.Disabled+",");
			}
			else
			{
				strSql.Append("Disabled= null ,");
			}
			if (model.Indate != null)
			{
				strSql.Append("Indate='"+model.Indate+"',");
			}
			else
			{
				strSql.Append("Indate= null ,");
			}
			if (model.InUser != null)
			{
				strSql.Append("InUser='"+model.InUser+"',");
			}
			else
			{
				strSql.Append("InUser= null ,");
			}
			if (model.EditDate != null)
			{
				strSql.Append("EditDate='"+model.EditDate+"',");
			}
			else
			{
				strSql.Append("EditDate= null ,");
			}
			if (model.EditUser != null)
			{
				strSql.Append("EditUser='"+model.EditUser+"',");
			}
			else
			{
				strSql.Append("EditUser= null ,");
			}
			int n = strSql.ToString().LastIndexOf(",");
			strSql.Remove(n, 1);
			strSql.Append(" where SysNo="+ model.SysNo+"");
			int rowsAffected=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rowsAffected > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int SysNo)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from PrimaryTumor ");
			strSql.Append(" where SysNo="+SysNo+"" );
			int rowsAffected=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rowsAffected > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string SysNolist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from PrimaryTumor ");
			strSql.Append(" where SysNo in ("+SysNolist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public HospitalManagement.Model.PrimaryTumor GetModel(int SysNo)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1  ");
			strSql.Append(" SysNo,PatientSysNo,HospitalName,SurgicalApproach,ResectionSurgery,Tumor,TumorTypes,SickNumber,SickText,NecrosisRate,NecrosisRateVal,Surgeon,Disabled,Indate,InUser,EditDate,EditUser ");
			strSql.Append(" from PrimaryTumor ");
			strSql.Append(" where SysNo="+SysNo+"" );
			HospitalManagement.Model.PrimaryTumor model=new HospitalManagement.Model.PrimaryTumor();
			DataSet ds=DbHelperSQL.Query(strSql.ToString());
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public HospitalManagement.Model.PrimaryTumor DataRowToModel(DataRow row)
		{
			HospitalManagement.Model.PrimaryTumor model=new HospitalManagement.Model.PrimaryTumor();
			if (row != null)
			{
				if(row["SysNo"]!=null && row["SysNo"].ToString()!="")
				{
					model.SysNo=int.Parse(row["SysNo"].ToString());
				}
				if(row["PatientSysNo"]!=null && row["PatientSysNo"].ToString()!="")
				{
					model.PatientSysNo=int.Parse(row["PatientSysNo"].ToString());
				}
				if(row["HospitalName"]!=null)
				{
					model.HospitalName=row["HospitalName"].ToString();
				}
				if(row["SurgicalApproach"]!=null)
				{
					model.SurgicalApproach=row["SurgicalApproach"].ToString();
				}
				if(row["ResectionSurgery"]!=null)
				{
					model.ResectionSurgery=row["ResectionSurgery"].ToString();
				}
				if(row["Tumor"]!=null)
				{
					model.Tumor=row["Tumor"].ToString();
				}
				if(row["TumorTypes"]!=null)
				{
					model.TumorTypes=row["TumorTypes"].ToString();
				}
				if(row["SickNumber"]!=null)
				{
					model.SickNumber=row["SickNumber"].ToString();
				}
				if(row["SickText"]!=null)
				{
					model.SickText=row["SickText"].ToString();
				}
				if(row["NecrosisRate"]!=null)
				{
					model.NecrosisRate=row["NecrosisRate"].ToString();
				}
				if(row["NecrosisRateVal"]!=null)
				{
					model.NecrosisRateVal=row["NecrosisRateVal"].ToString();
				}
				if(row["Surgeon"]!=null)
				{
					model.Surgeon=row["Surgeon"].ToString();
				}
				if(row["Disabled"]!=null && row["Disabled"].ToString()!="")
				{
					model.Disabled=int.Parse(row["Disabled"].ToString());
				}
				if(row["Indate"]!=null && row["Indate"].ToString()!="")
				{
					model.Indate=DateTime.Parse(row["Indate"].ToString());
				}
				if(row["InUser"]!=null)
				{
					model.InUser=row["InUser"].ToString();
				}
				if(row["EditDate"]!=null && row["EditDate"].ToString()!="")
				{
					model.EditDate=DateTime.Parse(row["EditDate"].ToString());
				}
				if(row["EditUser"]!=null)
				{
					model.EditUser=row["EditUser"].ToString();
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select SysNo,PatientSysNo,HospitalName,SurgicalApproach,ResectionSurgery,Tumor,TumorTypes,SickNumber,SickText,NecrosisRate,NecrosisRateVal,Surgeon,Disabled,Indate,InUser,EditDate,EditUser ");
			strSql.Append(" FROM PrimaryTumor ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" SysNo,PatientSysNo,HospitalName,SurgicalApproach,ResectionSurgery,Tumor,TumorTypes,SickNumber,SickText,NecrosisRate,NecrosisRateVal,Surgeon,Disabled,Indate,InUser,EditDate,EditUser ");
			strSql.Append(" FROM PrimaryTumor ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM PrimaryTumor ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.SysNo desc");
			}
			strSql.Append(")AS Row, T.*  from PrimaryTumor T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		*/

		#endregion  Method
		#region  MethodEx

		#endregion  MethodEx
	}
}

